﻿//////////////////////////////////////////////////////////////////////////
// Pre_Conditions: There must exist a "student" in the "session"
// Functions: For a student to apply for teachers
//////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAO;
using System.Data;

public partial class ApplyPage : System.Web.UI.Page
{
    // To control the current teacher count.
    private int iApplayNum = 0;
    private string strFirstPlan;
    private string strSecondPlan;
    private string strThirdPlan;

    private DataSet dsStudentPlan;
    
    protected void Page_Load(object sender, EventArgs e)
    {
        // for debug
        //Session["student"] = "888888";
        
        if (!IsPostBack)
        {
            BindTeachers();
        }
        if (IsPostBack)
        {
            createControls();
        }
    }

    protected void createControls()
    {
        StudentDAO studentDao = new StudentDAO();
        string strStudentId = (string)Session["studentId"];
        dsStudentPlan = studentDao.getStudentPlanByStudentID(strStudentId);
        DataRow rowPlan = dsStudentPlan.Tables[0].Rows[0];
        strFirstPlan = rowPlan[0].ToString().Trim();
        strSecondPlan = rowPlan[1].ToString().Trim();
        strThirdPlan = rowPlan[2].ToString().Trim();

        // To set the teacher number the student has chosen
        if (strFirstPlan == "")
            iApplayNum = 0;
        else if (strSecondPlan == "")
            iApplayNum = 1;
        else if (strThirdPlan == "")
            iApplayNum = 2;
        else
            iApplayNum = 3;

        foreach (GridViewRow row in gvTeachers.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                string strTeacherID = gvTeachers.DataKeys[row.RowIndex].Value.ToString().Trim();

                if (strFirstPlan == strTeacherID)
                {
                    Label lbPlan = new Label();
                    lbPlan.ID = "lbPlan";
                    lbPlan.Text = "第一志愿";
                    lbPlan.ForeColor = System.Drawing.Color.Red;
                    row.Cells[5].Controls.Add(lbPlan);
                    LinkButton lbCancel = new LinkButton();
                    lbCancel.ID = "lbCancel";
                    lbCancel.Text = "退出";
                    lbCancel.ForeColor = System.Drawing.Color.Blue;
                    lbCancel.CommandName = "cmdCancel";
                    lbCancel.CommandArgument = "1";
                    row.Cells[5].Controls.Add(lbCancel);
                }
                else if (strSecondPlan == strTeacherID)
                {
                    Label lbPlan = new Label();
                    lbPlan.ID = "lbPlan";
                    lbPlan.Text = "第二志愿";
                    lbPlan.ForeColor = System.Drawing.Color.Red;
                    row.Cells[5].Controls.Add(lbPlan);
                    LinkButton lbCancel = new LinkButton();
                    lbCancel.ID = "lbCancel";
                    lbCancel.Text = "退出";
                    lbCancel.ForeColor = System.Drawing.Color.Blue;
                    lbCancel.CommandName = "cmdCancel";
                    lbCancel.CommandArgument = "2";
                    row.Cells[5].Controls.Add(lbCancel);
                }
                else if (strThirdPlan == strTeacherID)
                {
                    Label lbPlan = new Label();
                    lbPlan.ID = "lbPlan";
                    lbPlan.Text = "第三志愿";
                    lbPlan.ForeColor = System.Drawing.Color.Red;
                    row.Cells[5].Controls.Add(lbPlan);

                    LinkButton lbCancel = new LinkButton();
                    lbCancel.ID = "lbCancel";
                    lbCancel.Text = "退出";
                    lbCancel.ForeColor = System.Drawing.Color.Blue;
                    lbCancel.CommandName = "cmdCancel";
                    lbCancel.CommandArgument = "3";
                    row.Cells[5].Controls.Add(lbCancel);
                }
                else
                {
                    LinkButton lbApply = new LinkButton();
                    lbApply.ID = "lbApply";
                    lbApply.Text = "申请";
                    lbApply.ForeColor = System.Drawing.Color.Blue;
                    lbApply.CommandName = "cmdApply";
                    // the argument is the teacher's ID
                    lbApply.CommandArgument = strTeacherID;

                    if (row.Cells[3].Text.Trim() == row.Cells[4].Text.Trim())
                    {
                        lbApply.Enabled = false;
                    }
                    row.Cells[5].Controls.Add(lbApply);
                }
            }
        }
    }

    // To add item to the last cell of each row
    protected void TeachersView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        StudentDAO studentDao = new StudentDAO();
        string strStudentId = (string)Session["studentId"];
        dsStudentPlan = studentDao.getStudentPlanByStudentID(strStudentId);
        DataRow rowPlan = dsStudentPlan.Tables[0].Rows[0];
        strFirstPlan = rowPlan[0].ToString().Trim();
        strSecondPlan = rowPlan[1].ToString().Trim();
        strThirdPlan = rowPlan[2].ToString().Trim();

        // To set the teacher number the student has chosen
        if (strFirstPlan == "")
            iApplayNum = 0;
        else if (strSecondPlan == "")
            iApplayNum = 1;
        else if (strThirdPlan == "")
            iApplayNum = 2;
        else
            iApplayNum = 3;

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string strTeacherID = gvTeachers.DataKeys[e.Row.RowIndex].Value.ToString().Trim();

            if (strFirstPlan == strTeacherID)
            {
                Label lbPlan = new Label();
                lbPlan.ID = "lbPlan";
                lbPlan.Text = "第一志愿";
                lbPlan.ForeColor = System.Drawing.Color.Red;
                e.Row.Cells[5].Controls.Add(lbPlan);
                LinkButton lbCancel = new LinkButton();
                lbCancel.ID = "lbCancel";
                lbCancel.Text = "退出";
                lbCancel.ForeColor = System.Drawing.Color.Blue;
                lbCancel.CommandName = "cmdCancel";
                lbCancel.CommandArgument = "1";
                e.Row.Cells[5].Controls.Add(lbCancel);
            }
            else if (strSecondPlan == strTeacherID)
            {
                Label lbPlan = new Label();
                lbPlan.ID = "lbPlan";
                lbPlan.Text = "第二志愿";
                lbPlan.ForeColor = System.Drawing.Color.Red;
                e.Row.Cells[5].Controls.Add(lbPlan);
                LinkButton lbCancel = new LinkButton();
                lbCancel.ID = "lbCancel";
                lbCancel.Text = "退出";
                lbCancel.ForeColor = System.Drawing.Color.Blue;
                lbCancel.CommandName = "cmdCancel";
                lbCancel.CommandArgument = "2";
                e.Row.Cells[5].Controls.Add(lbCancel);
            }
            else if (strThirdPlan == strTeacherID)
            {
                Label lbPlan = new Label();
                lbPlan.ID = "lbPlan";
                lbPlan.Text = "第三志愿";
                lbPlan.ForeColor = System.Drawing.Color.Red;
                e.Row.Cells[5].Controls.Add(lbPlan);

                LinkButton lbCancel = new LinkButton();
                lbCancel.ID = "lbCancel";
                lbCancel.Text = "退出";
                lbCancel.ForeColor = System.Drawing.Color.Blue;
                lbCancel.CommandName = "cmdCancel";
                lbCancel.CommandArgument = "3";
                e.Row.Cells[5].Controls.Add(lbCancel);
            }
            else
            {
                LinkButton lbApply = new LinkButton();
                lbApply.ID = "lbApply";
                lbApply.Text = "申请";
                lbApply.ForeColor = System.Drawing.Color.Blue;
                lbApply.CommandName = "cmdApply";
                // the argument is the teacher's ID
                lbApply.CommandArgument = strTeacherID;

                if (e.Row.Cells[3].Text.Trim() == e.Row.Cells[4].Text.Trim())
                {
                    lbApply.Enabled = false;
                }
                e.Row.Cells[5].Controls.Add(lbApply);
            }

        }
    }

    protected void BindTeachers()
    {
        TeacherDAO teacherDao = new TeacherDAO();
        DataSet ds = teacherDao.getAllTeachersAndPlan();
        gvTeachers.DataSource = ds.Tables[0];

        StudentDAO studentDao = new StudentDAO();
        string strStudentId = (string)Session["studentId"];
        dsStudentPlan = studentDao.getStudentPlanByStudentID(strStudentId);
        DataRow rowPlan = dsStudentPlan.Tables[0].Rows[0];
      
        strFirstPlan = rowPlan[0].ToString().Trim();
        strSecondPlan = rowPlan[1].ToString().Trim();
        strThirdPlan = rowPlan[2].ToString().Trim();
        // To set the teacher number the student has chosen
        if (strFirstPlan == "")
            iApplayNum = 0;
        else if (strSecondPlan == "")
            iApplayNum = 1;
        else if (strThirdPlan == "")
            iApplayNum = 2;
        else
            iApplayNum = 3;

        gvTeachers.DataBind();
    }


    protected void gvTeachers_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string stuID = Session["studentId"].ToString().Trim();
        StudentDAO studentDao = new StudentDAO();
        string strTeacherID = e.CommandArgument.ToString();

        if (e.CommandName == "cmdApply")
        {

            if (false == studentDao.applyForTeacher(stuID, strTeacherID, ++iApplayNum))
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('申请失败。')</script>");
                return;
            }
            this.BindTeachers();
        }
        // 取消
        else if (e.CommandName == "cmdCancel")
        {
            // If it is a cancel button, the argument is the plan number
            int iCancelNum = Convert.ToInt32(strTeacherID);
            //
            int index = ((GridViewRow)((LinkButton)(e.CommandSource)).Parent.Parent).RowIndex;
            strTeacherID = this.gvTeachers.DataKeys[index].Value.ToString();
            if (false == studentDao.cancelPlan(stuID, strTeacherID, --iApplayNum, iCancelNum))
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('退出失败 。')</script>");
                return;
            }
            this.BindTeachers();
        }

        else if (e.CommandName == "cmdDetail")
        {
            Response.Redirect("~/Student/TeacherDescription.aspx?teacherId=" + e.CommandArgument.ToString().Trim() + "");
        }

    }
    protected void Button1_Click(object sender, EventArgs e)
    {

    }
}
